package service;

import mapper.ConductorMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.Staff;
import util.SqlSessionFactoryUtil;

import java.util.List;

public class ConductorService {
    private final SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    /**
     * 查询员工是否存在，根据员工对象staff的所有属性查询
     * @param staff员工对象
     * @return
     */
    public boolean conductorLogin(Staff staff) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ConductorMapper mapper = sqlSession.getMapper(ConductorMapper.class);

        List<Staff> staffs = mapper.selectOneOfConductorOfPassword(staff);

        sqlSession.close();
        return !staffs.isEmpty();
    }

    /**
     * 根据数据传输对象DTO，查询一个员工
     * @param staff（当时没有使用springboot，因此通过持久层对象po模拟数据传输对象dto）
     * @return
     */
    public Staff selectOneConductor(Staff staff) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ConductorMapper mapper = sqlSession.getMapper(ConductorMapper.class);

        List<Staff> staffs = mapper.selectOneConductorNo(staff.get_staffNo());

        sqlSession.close();

        return staffs.isEmpty() ? null : staffs.get(0);
    }

    /**
     * 更新员工信息
     * @param staff
     */
    public void updateConductorBaseInfo(Staff staff) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ConductorMapper mapper = sqlSession.getMapper(ConductorMapper.class);

        mapper.updateInfo(staff);

        sqlSession.commit();
        sqlSession.close();
    }
}
